package thread;

import java.util.Scanner;

public class Demo4 {

    //内存可见性 volatile 关键字 确保变量的可见性
    public volatile static int flg = 0;
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (flg == 0) {
                System.out.println("111");
                try {
                    Thread.sleep(1000);//避免优化
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.printf("t1 线程结束");
        });
        Thread t2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入数字");
            scanner.nextInt();
            flg = 1;
            System.out.println("t2 线程修改flg 为1");
        });
        t1.start();
        t2.start();
    }
}
